<?php
/**
 * 陈杰  18323491246
 */

namespace App\Http\Controllers;

use App\Models\Course\CourseBuyModel;
use App\Models\Course\CourseModel;
use App\Models\Course\CourseSubModel;
use Illuminate\Http\Request;

class CourseController extends Controller
{

    public function buyList(Request $request)
    {
        $id = (int)$request->input('course_id');
        $data = CourseBuyModel::where('course_id', $id)
            ->join('member', 'course_buy.member_id', '=', 'member.id')
            ->select('course_buy.*', 'member.phone as member_phone', 'member.name as member_name')
            ->orderBy('id', 'desc')
            ->paginate($request->get('count'));
        return self::returnMsg($data);
    }

    public function subEdit(Request $request)
    {
        $rules  = [
            'id'     => 'required|integer|min:1',
            'status' => 'required|integer|min:0',
        ];
        $sqlmap = self::checkValidate($rules, $request->all());

        $model         = CourseSubModel::find($sqlmap['id']);
        $model->status = $sqlmap['status'];
//        $model->money  = $sqlmap['money'];
        if (!isset($model))
            return self::errorMsg('没有找到该课程');
        try {
            $model->save();
            return self::successMsg();
        } catch (\Exception $exception) {
            return self::errorMsg($exception->getMessage());
        }
    }

    public function subList(Request $request)
    {
        $firstWhere = function ($query) use ($request) {
            if ($request->has('course_id') and $request->course_id != '') {
                $query->where('course_sub.course_id', $request->course_id);
            }
            if ($request->has('status') and $request->status != '') {
                $query->where('course_sub.status', $request->status);
            }
        };

        $where = function ($query) use ($request) {
            //筛选查询关键字
            if ($request->has('keywords') and $request->keywords != '') {
                $keywords = "%" . $request->keywords . "%";
                $query->where('course_sub.id', '=', $request->keywords)
                    ->orWhere('course_sub.title', 'like', $keywords)
                    ->orWhere('course_sub.member_id', '=', $request->keywords)
                    ->orWhere('member.phone', '=', $request->keywords)
                    ->orWhere('member.name', '=', $request->keywords);
            }
        };
        $order = 'id';
        if ($request->has('order') and $request->order != '') {
            $order = 'course_sub.' . $request->order;
        }
        $data = CourseSubModel::where($firstWhere)
            ->where($where)
            ->join('member', 'course_sub.member_id', '=', 'member.id')
            ->join('course', 'course_sub.course_id', '=', 'course.id')
            ->orderByDesc($order)
            ->select('course_sub.*', 'course.title as course_title', 'member.phone as member_phone', 'member.name as member_name')
            ->paginate($request->get('count'));
        return self::returnMsg($data);
    }

    public function courseList(Request $request)
    {
        $firstWhere = function ($query) use ($request) {
            if ($request->has('status') and $request->status != '') {
                $query->where('course.status', $request->status);
            }
            if ($request->has('role') and $request->role != '') {
                $query->where('course.role', $request->role);
            }
        };

        $where = function ($query) use ($request) {
            //筛选查询关键字
            if ($request->has('keywords') and $request->keywords != '') {
                $keywords = "%" . $request->keywords . "%";
                $query->where('course.id', '=', $request->keywords)
                    ->orWhere('course.member_id', '=', $request->keywords)
                    ->orWhere('course.title', 'like', $keywords)
                    ->orWhere('member.phone', '=', $request->keywords)
                    ->orWhere('member.name', '=', $request->keywords);
            }
        };
        $order = 'id';
        if ($request->has('order') and $request->order != '') {
            $order = 'course.' . $request->order;
        }
        $data = CourseModel::where($firstWhere)->where($where)
            ->join('member', 'course.member_id', '=', 'member.id')
            ->orderByDesc($order)
            ->select('course.*', 'member.phone as member_phone', 'member.name as member_name')
            ->paginate($request->get('count'));
        return self::returnMsg($data);
    }

    public function courseEdit(Request $request)
    {
        $rules  = [
            'id'     => 'required|integer|min:1',
            'money'  => 'required|numeric|min:0',
            'status' => 'required|integer|min:0',
        ];
        $sqlmap = self::checkValidate($rules, $request->all());

        $model         = CourseModel::find($sqlmap['id']);
        $model->status = $sqlmap['status'];
        $model->money  = $sqlmap['money'];
        if (!isset($model))
            return self::errorMsg('没有找到该课程');
        try {
            $model->save();
            return self::successMsg();
        } catch (\Exception $exception) {
            return self::errorMsg($exception->getMessage());
        }
    }
}
